package com.soft.dsmp.service;

import com.soft.dsmp.GenConfig;
import com.soft.dsmp.dao.SysGeneratorDao;
import com.soft.dsmp.entity.ColumnEntity;
import com.soft.dsmp.entity.TableEntity;
import com.soft.dsmp.utils.Constants;
import com.soft.dsmp.utils.GenUtils;
import com.soft.dsmp.utils.StringUtils;
import com.soft.dsmp.utils.VelocityInitializer;
import org.apache.commons.io.IOUtils;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

/**
 * 代码生成器
 * 
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2016年12月19日 下午3:33:38
 */
@Service
public class SysGeneratorService {
	@Autowired
	private SysGeneratorDao sysGeneratorDao;

	public List<Map<String, Object>> queryList(Map<String, Object> map) {
		return sysGeneratorDao.queryList(map);
	}

	public int queryTotal(Map<String, Object> map) {
		return sysGeneratorDao.queryTotal(map);
	}

	public Map<String, String> queryTable(String tableName) {
		return sysGeneratorDao.queryTable(tableName);
	}

	public List<Map<String, String>> queryColumns(String tableName) {
		return sysGeneratorDao.queryColumns(tableName);
	}

	public byte[] generatorCode(String[] tableNames) {
		ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
		ZipOutputStream zip = new ZipOutputStream(outputStream);
		for (String tableName : tableNames)
		{
			//查询表信息
			Map<String, String> table = queryTable(tableName);
			//查询列信息
			List<Map<String, String>> columns = queryColumns(tableName);
			// 生成代码
			generatorCode(table, columns, zip);
		}
		IOUtils.closeQuietly(zip);
		return outputStream.toByteArray();
	}

	/**
	 * 生成代码
	 */
	public void generatorCode(Map<String, String> table, List<Map<String, String>> columns, ZipOutputStream zip)
	{

		TableEntity tableEntity = new TableEntity();
		tableEntity.setTableName(table.get("tableName"));
		tableEntity.setComments(table.get("tableComment"));
		//列信息
		List<ColumnEntity> columsList = new ArrayList<>();
		// 表名转换成Java属性名
		String className = GenUtils.tableToJava(tableEntity.getTableName());
		tableEntity.setClassName(className);
		tableEntity.setClassname(StringUtils.uncapitalize(className));
		tableEntity.setPackagename(className.toLowerCase());

		// 列信息
		for(Map<String, String> column : columns){
			ColumnEntity columnEntity = new ColumnEntity();
			columnEntity.setColumnName(column.get("columnName"));
			columnEntity.setDataType(column.get("dataType"));
			columnEntity.setComments(column.get("columnComment"));
			columnEntity.setExtra(column.get("extra"));
			columsList.add(columnEntity);
		}
		// 列信息
		tableEntity.setColumns(GenUtils.transColums(columsList));
		// 设置主键
		tableEntity.setPk(tableEntity.getColumns().get(0));

		VelocityInitializer.initVelocity();

		String packageName = GenConfig.getPackageName();
		String moduleName = GenUtils.getModuleName(packageName);

		VelocityContext context = GenUtils.getVelocityContext(tableEntity);

		// 获取模板列表
		List<String> templates = GenUtils.getTemplates();
		for (String template : templates)
		{
			// 渲染模板
			StringWriter sw = new StringWriter();
			Template tpl = Velocity.getTemplate(template, Constants.UTF8);
			tpl.merge(context, sw);
			try
			{
				// 添加到zip
				zip.putNextEntry(new ZipEntry(GenUtils.getFileName(template, tableEntity, moduleName)));
				IOUtils.write(sw.toString(), zip, Constants.UTF8);
				IOUtils.closeQuietly(sw);
				zip.closeEntry();
			}
			catch (IOException e)
			{
				e.printStackTrace();
			}
		}
	}

}
